package com.Aliyun.OCSCache;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.BinaryConnectionFactory;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;

import com.alibaba.fastjson.JSON;
import com.limao.Tool.CacheClass;

/*
 *   不用 
 */
public class OCSTools<T> {

	//private final String host = "acca8d7680f4481b.m.cnhzaliqshpub001.ocs.aliyuncs.com";// 控制台上的“内网地址”
	private final String host = "0d0189059a104022.m.cnszalist3pub001.ocs.aliyuncs.com";
	private final String port = "11211"; // 默认端口 11211，不用改
	private final String username = "0d0189059a104022";//"acca8d7680f4481b";// 控制台上的“访问账号“
	private final String password = "Abcd262610438";//"Gllk2015";// 邮件或短信中提供的“密码

	private MemcachedClient cache = null;
	private OperationFuture future;
	private String CacheTypeName = "";
	private int lifecycleTime = 24*3600; //默认值3000秒 5分钟

	Class<T> clazz;

	//private String address=host + ":" + port;
	
	
	private String getUrlAddress()
	{
		if(CacheClass.isOut==2)
			return "192.168.0.177:11211";
		else if(CacheClass.isOut==1)
			return "192.168.8.1:11211 192.168.8.2:11211";
		else 
			return "192.168.8.1:11211 192.168.8.2:11211";
	}
	
	public OCSTools(String CacheTypeName,int lifecycleTime)
	{
		this.CacheTypeName = CacheTypeName;
		this.lifecycleTime = lifecycleTime;
       // if(Function.isOut==2) return;
		if (cache != null)
			return;

		try {
			// 免验证模式
			cache = new MemcachedClient(new BinaryConnectionFactory(),AddrUtil.getAddresses(getUrlAddress()));

			System.out.println("OCS Cache建立成功……");

		} catch (IOException e) {
			System.out.println("OCS Cache建立失败……");
			e.printStackTrace();
			if (cache != null) {
				cache.shutdown();
			}

		}
	}
	
	
	public OCSTools(String CacheTypeName, Class<T> clazz)
	{
		this.CacheTypeName = CacheTypeName;
		this.clazz = clazz;
       // if(Function.isOut==2) return;
		if (cache != null)
			return;

		try {
			// 免验证模式
			cache = new MemcachedClient(new BinaryConnectionFactory(),AddrUtil.getAddresses(getUrlAddress()));

			System.out.println("OCS Cache建立成功……");
			/*
			 * 验证模式 AuthDescriptor ad = new AuthDescriptor(new
			 * String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
			 * cache = new MemcachedClient(new ConnectionFactoryBuilder()
			 * .setProtocol(Protocol.BINARY) .setAuthDescriptor(ad) .build(),
			 * AddrUtil.getAddresses(host + ":" + port));
			 */

		} catch (IOException e) {
			System.out.println("OCS Cache建立失败……");
			e.printStackTrace();
			if (cache != null) {
				cache.shutdown();
			}

		}
	}
	
	
	public OCSTools(String CacheTypeName, int lifecycleTime, Class<T> clazz) {
		this.CacheTypeName = CacheTypeName;
		this.lifecycleTime = lifecycleTime;
		this.clazz = clazz;
		//if(Function.isOut==2) return;
		if (cache != null)
			return;

		try {
			// 免验证模式
			cache = new MemcachedClient(new BinaryConnectionFactory(),AddrUtil.getAddresses(getUrlAddress()));
			System.out.println("OCS Cache建立成功……");
			/*
			 * 验证模式 AuthDescriptor ad = new AuthDescriptor(new
			 * String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
			 * cache = new MemcachedClient(new ConnectionFactoryBuilder()
			 * .setProtocol(Protocol.BINARY) .setAuthDescriptor(ad) .build(),
			 * AddrUtil.getAddresses(host + ":" + port));
			 */

		} catch (IOException e) {
			System.out.println("OCS Cache建立失败……");
			e.printStackTrace();
			if (cache != null) {
				cache.shutdown();
				//cache=null;
			}

		}
	}

	public <T> T get(String key) {
	//	if(Function.isOut==2) return null;
		if (cache == null)	return null;
		
		String str=null;
		try {

			//str=cache.getAndTouch(CacheTypeName + "_" + key, 30);
			str=(String) cache.get(CacheTypeName + "_" + key);
		} catch (Exception e) {
			e.printStackTrace();

		}
		
		if(str==null)
			return null;
		else
		{
			System.out.println("OCS getT key:"+CacheTypeName+ "_" + key+", "+str);
			
			return (T) JSON.parseObject(str, clazz);
		}
			
	}

	/*
	 * public static final <T> T parseObject(String text, Class<T> clazz) {
	 * return (T) parseObject(text, clazz, new Feature[0]); }
	 */

	public void put(String key, Object user) {
		//if(Function.isOut==2) return;
		if (cache == null)
			return;

		String value = JSON.toJSONString(user);
		try {
			future = cache.set(CacheTypeName + "_" + key, lifecycleTime, value);
			// 确保之前set()操作已经结束
			future.get();
			System.out.println("OCS putT 成功:"+CacheTypeName+ "_" + key+", "+value);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (Exception e) 
		{
			e.printStackTrace();
		}

	}

	public void clear(String key) {
		//if(Function.isOut==2) return;
		if (cache == null)
			return;
		
		try {
			future=cache.delete(CacheTypeName + "_" + key);
			// 确保之前set()操作已经结束
			future.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
			cache=null;
		} catch (ExecutionException e) {
			e.printStackTrace();
			cache=null;
		}catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
//***********************************************************
	

	public List<T>  getList(String key) {
		//if(Function.isOut==2) return null;
		if (cache == null)	return null;
		
		String str=null;
		try {

			//str= cache.getAndTouch(CacheTypeName + "_" + key,30);
			str= (String) cache.get(CacheTypeName + "_" + key);
		} catch (Exception e) {
			e.printStackTrace();

		}
		
		if(str==null)
			return null;
		else
		{
			System.out.println("OCS getList key:"+CacheTypeName+ "_" + key+", "+str);
			return JSON.parseArray(str, clazz);
		}
			
	}
	
	
	public void putList(String key, List list) {
		//if(Function.isOut==2) return ;
		if (cache == null)
			return;
		String str=JSON.toJSONString(list, true);
		try {
			future = cache.set(CacheTypeName + "_" + key, lifecycleTime, str);
			// 确保之前set()操作已经结束
			future.get();
			System.out.println("OCS putList 成功:"+CacheTypeName+ "_" + key+", "+str);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (Exception e) 
		{
			e.printStackTrace();
		}

	}
	
	
	
//************************************************************
	public String getStr(String key) {
		//if(Function.isOut==2) return null;
		if (cache == null)	return null;
		
		String str=null;
		try {
			System.out.println("OCS getStr:"+CacheTypeName+ "_" + key);
			//str=cache.getAndTouch(CacheTypeName + "_" + key,30);
			str=(String) cache.get(CacheTypeName + "_" + key);
		} catch (Exception e) {
			System.out.println("OCS getStr:Exception ");
			e.printStackTrace();

		}
		if(str==null)
			return null;
		else
		{
			System.out.println("OCS getStr:"+CacheTypeName+ "_" + key+", "+str);
			return str;
		}

	}

	/*
	 * public static final <T> T parseObject(String text, Class<T> clazz) {
	 * return (T) parseObject(text, clazz, new Feature[0]); }
	 */

	public void putStr(String key, String str) {
	//	if(Function.isOut==2) return ;
		if (cache == null)
			return;

		try {
			future = cache.set(CacheTypeName + "_" + key, lifecycleTime, str);
			// 确保之前set()操作已经结束
			future.get();
			System.out.println("OCS putStr 成功:"+CacheTypeName+ "_" + key+", "+str);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (Exception e) 
		{
			e.printStackTrace();
		}

	}

	
	
}
